﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class Album : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "Album";
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Album ảnh");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override WebPartInt[] SubWebParts
        {
            get
            {
                WebPartInt[] SubWebParts = new WebPartInt[]{
                    new AddAlbum(),new UpdateAlbum(),new AlbumItems(),new AddAlbumItem(), new AlbumUploadAvatar()
                };
                return SubWebParts;
            }
        }


        public string GetViewAlbumUrl(string belongSite, string Subject, string ShortId)
        {
            Subject = Environments.ConvertSubject(Subject);
            string Url = Environments.HttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + Subject + "-vfa-" + ShortId + "." + Environments.Extension;
            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string belongSite)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                Environments.CheckWebPartAllow(WebPartId, true);

                string SelectSite =
                    "<select id=drpSelectSiteForAlbum onchange=javascript:FilterFunc();>";
                string[] Sites = Environments.GetSites();
                for (int iIndex = 0; iIndex < Sites.Length; iIndex++)
                {
                    if (Sites[iIndex].ToLower().Equals(belongSite))
                    {
                        SelectSite += "<option selected value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                    else
                    {
                        SelectSite += "<option value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                }
                SelectSite += "</select>";
                DataTable dtAlbum = new AlbumBLL().Reading(belongSite);
                string RetHtml =
                    "   <div>" + ResourceBLL.GetText(WebPartId, "SelectSite", "Site") + ":" + SelectSite + "&nbsp;<input type=button value=\"" + ResourceBLL.GetText(WebPartId, "Go", "Lọc dữ liệu") + "\" onclick=javascript:FilterFunc();>&nbsp;<input type=button onclick=\"javascript:ClientAddAlbum();\" value=\"" + ResourceBLL.GetText(WebPartId, "TH_Add", "Thêm") + "\"></div>" +
                    "   <div id=divProcessing></div>";

                if (dtAlbum.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=
                                        "   <table>" +
                                        "       <tr>" +
                                        "           <th>" + ResourceBLL.GetText(WebPartId, "TH_Subject", "Tiêu đề") + "</th>" +
                                        "           <th></th>" +
                                        "           <th></th>" +
                                        "           <th></th>" +
                                        //"           <th></th>" +
                           //             "           <th></th>" +
                                        "       </tr>";


                    for (int iIndex = 0; iIndex < dtAlbum.Rows.Count; iIndex++)
                    {
                        string AlbumId = (string)dtAlbum.Rows[iIndex]["AlbumId"];
                        string Subject = "";
                        try
                        {
                            Subject = (string)dtAlbum.Rows[iIndex][Environments.ActiveLanguage + "Subject"];
                        }
                        catch { }

                        string AlbumType = "";
                        try
                        {
                            AlbumType = (string)dtAlbum.Rows[iIndex]["AlbumType"];
                        }
                        catch { }
                        string DeleteUrl = "javascript:ClientDeleteAlbum('" + AlbumId + "')";
                        string LinkText = GetViewAlbumUrl(belongSite, Subject, AlbumId.Substring(0, 8));
                        LinkText = LinkText.Substring(LinkText.LastIndexOf("/") + 1);
                        LinkText = "<a target=\"_blank\" href=\"" + Environments.HttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + LinkText + "\">" + LinkText + "</a>";
                        string SyncUrl = "javascript:ClientSyncWithServer('" + AlbumId + "')";

                        RetHtml +=
                        "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                        "           <td class=cssTdSubjectAlbum>" + Subject + "</td>" +
                            //"           <td class=cssTdSubjectAlbum>" + AlbumType + "</td>" +
                        "           <td><a href=\"javascript:ShowClientUpdateUrlForm('" + AlbumId + "');\">" + ResourceBLL.GetText(WebPartId, "TH_Update", "Cập nhật") + "</a></td>" +
                        "           <td><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Delete", "Xóa") + "</a></td>" +
                        "           <td><a href=\"javascript:ShowClientAlbumItemUrlForm('" + AlbumId + "');\">" + ResourceBLL.GetText(WebPartId, "ShowAlbumItemCaption", "Xem chi tiết") + "</a></td>" +
                            //"           <td><a href=\"" + SyncUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Sync", "Sync") + "</a></td>" +
                        "       </tr>";
                    }
                    RetHtml +=
                        "   </table>";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Album));
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "Album.js") +
                "<div id=divAlbumMain class=cssDivAlbum>" + GetHtml("vie") + "</div>";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetAddGroupNewUrl(string belongSite)
        {
            return Screen.BuildDialogUrl(new ParamCls[]{
                new ParamCls("WebPartId",new AddAlbum().WebPartId),
                new ParamCls("belongSite",belongSite)
            });
        }
      

        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId,"ConfirmDeleteMsg","Bạn đã chắc chắn xóa chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmPublishingMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmPublishingMsg", "Bạn đã chắc chắn phát hành lên website public chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteAlbum(string AlbumId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                
                SqlTransaction sqlTransaction = Environment.Environments.CreateTransaction();
                try
                {
                    new AlbumBLL().DeleteAlbum(sqlTransaction, AlbumId);
                    sqlTransaction.Commit();
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    throw (ex);
                }
                return ResourceBLL.GetText(WebPartId,"DeleteOkMessage","Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string AlbumId)
        {
            string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateAlbum().WebPartId),
                        new ParamCls("AlbumId",AlbumId)
                    });
            return UpdateUrl;
        }


        [AjaxPro.AjaxMethod]
        public string GetShowNewsItemForm(string AlbumId)
        {
            string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId","News"),
                        new ParamCls("AlbumId",AlbumId)
                    });
            return UpdateUrl;
        }

        [AjaxPro.AjaxMethod]
        public string GetShowAlbumItemsForm(string AlbumId)
        {
            string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AlbumItems().WebPartId),
                        new ParamCls("AlbumId",AlbumId)
                    });
            return UpdateUrl;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSidePublishing(string AlbumId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
               // new AlbumBLL().PublishingPublicWebSite(AlbumId);
                return ResourceBLL.GetText(WebPartId, "PublishingOk", "Xuất bản lên website thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }            
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }




        [AjaxPro.AjaxMethod]
        public string ServerSideSyncWithServer(string AlbumId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                new AlbumBLL().SyncWithServer(AlbumId);
                return GetText("SyncOk", "Đồng bộ với máy chủ web thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncAllMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncAllMsg", "Bạn đã chắc chắn update lại dữ liệu tin tức trên máy chủ từ máy local chưa?");
        }
    }
}
